上傳檔案 - MQTT

需先安裝 ampy ( Adafruit MicroPython Tool )

pip install adafruit-ampy

https://github.com/adafruit/ampy


In [1]:
import os, sys
sys.path.append(os.path.join('..', '..', '..', '..', '..', ))

import ampy_utils
# with open(os.path.join('..', '..', '..', '..', '..', 'ampy_utils.py'), 'r') as f:
#     print(f.read())

設定COM port (set current COM port), baud rate


In [2]:
ampy_utils.baud_rate = 115200
ampy_utils.com_port = 'COM3'
# ampy_utils.com_port = '/dev/ttyUSB0'

Load utility functions


In [3]:
from ampy_utils import *

Copy files and folders to device


In [5]:
root_folders = [os.path.sep.join(['..', '..', 'codes', 'micropython']),
                os.path.sep.join(['..', '..', 'codes', 'micropython_mqtt']),
                os.path.sep.join(['..', '..', 'codes', 'node']),
                os.path.sep.join(['..', '..', 'codes', 'shared']),]
folders = []

format_put_files_folders(root_folders = root_folders,
                         folders = folders, 
                         format_first = True)


[Formatting file system on device COM3]

[Clearing folder upload\py]

[Copying all files to upload folder upload\py]

[Copying all files to device COM3]
Deleting ['main.py', 'main.mpy']
Copying file: asynch_result.py
Copying file: boot.py
Copying file: commander.py
Copying file: config_mqtt.py
Copying file: config_mqtt_local.py
Copying file: data_transceiver.py
Copying file: hardware.py
Copying file: led.py
Copying file: mqtt_client_paho.py
Copying file: mqtt_client_umqtt.py
Copying file: node.py
Copying file: phonebook.py
Copying file: queue_manager.py
Copying file: robust.py
Copying file: simple.py
Copying file: socket_client.py
Copying file: u_python.py
Copying file: u_python_profiler.py
Copying file: watchdog.py
Copying file: webrepl_cfg.py
Copying file: worker.py
Copying file: worker_config.py
Copying file: worker_cpython.py
Copying file: worker_neuron.py
Copying file: worker_neuron_20170516.py
Copying file: worker_upython.py
Copying file: main.py

[Copying all folders to device COM3]

[All done!]

單一檔案上傳 (single file upload, in case needed)


In [5]:
# copy_one_file_to_device(os.path.sep.join(['..', '..', 'codes', 'micropython']), 'main.py')

In [6]:
# copy_one_file_to_device(os.path.sep.join(['..', '..', 'codes', 'shared']), 'config_mqtt.py')

In [7]:
# copy_one_file_to_device(os.path.sep.join(['..', '..', '..', 'dmz']), 'config_mqtt.py')

In [8]:
# copy_one_file_to_device(os.path.sep.join(['..', '..', 'codes', 'node']), 'node.py')

列出檔案 (list files)


In [9]:
# list_files_in_device()

檢查檔案內容 (check file content)


In [10]:
# cat_file_from_device('main.py')

連網測試 (network config and test)


In [11]:
# 連上網路
# import network; nic=network.WLAN(network.STA_IF); nic.active(False);  # disable network
# import network; nic=network.WLAN(network.STA_IF); nic.active(True); nic.connect('SSID','password');nic.ifconfig()
# import network; nic=network.WLAN(network.STA_IF); nic.ifconfig()
# import network; nic=network.WLAN(network.STA_IF);nic.ifconfig();nic.config('mac');nic.ifconfig((['mac',])

Run Broker container on Raspberry Pi

copy folder 'codes' to Raspberry Pi under folder '/data/elastic_network_of_things_with_micropython',
so Raspberry Pi has folder '/data/elastic_network_of_things_with_micropython/codes'
then run the command below on Raspberry Pi.

docker run -it -p 9662:9662 --name=Broker --hostname=Broker --volume=/data/elastic_network_of_things_with_micropython:/project wei1234c/python_armv7 /bin/sh -c "cd /project/codes/broker && python3 broker.py"